﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Org.BouncyCastle.Ocsp;

namespace FangChanCrm.Controllers
{
    public class YiJianFanKuiController : Controller
    {
        private readonly It_YiJianFanKuiBiaoService it_YiJianFanKuiBiaoService;
        private readonly It_RenYuanBiaoService it_RenYuanBiaoService;

        public YiJianFanKuiController(It_YiJianFanKuiBiaoService it_YiJianFanKuiBiaoService, It_RenYuanBiaoService it_RenYuanBiaoService, IConfiguration configuration)
        {
            this.it_YiJianFanKuiBiaoService = it_YiJianFanKuiBiaoService;
            this.it_RenYuanBiaoService = it_RenYuanBiaoService;
            Configuration = configuration;
        }

        //private readonly It_RenYuanBiaoService it_RenYuanBiaoService;

        public IConfiguration Configuration { get; }


        public IActionResult YiJianFanKuiList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }


        #region 列表
        [HttpPost]
        public string GetListWithFenYe()
        {
            t_YiJianFanKuiBiao t_YiJianFanKuiBiao = new t_YiJianFanKuiBiao();
            t_YiJianFanKuiBiao.RenYuanId = Convert.ToInt32(Request.Form["ipt_YuanGongMingCheng"]);
            t_YiJianFanKuiBiao.ZhuangTai = Request.Form["slt_ZhuangTai"];
            if (Request.Form["ipt_ShiJian"] != "")
            {
                t_YiJianFanKuiBiao.ShiJian = Convert.ToDateTime(Request.Form["ipt_ShiJian"]).ToLongDateString().ToString();
            }
            return it_YiJianFanKuiBiaoService.GetListWithFenYe(t_YiJianFanKuiBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }
        #endregion
        #region 人员下拉框
        public string BangDingRenYuan()
        {
            return JsonConvert.SerializeObject(it_RenYuanBiaoService.GetList(), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        #endregion
        #region 绑
        public string GetById()
        {
            return JsonConvert.SerializeObject(new List<t_YiJianFanKuiBiao> { it_YiJianFanKuiBiaoService.GetById(Convert.ToInt32(Request.Form["Id"])) }, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        #endregion
        #region 修改
        public int Update()
        {
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var updatebase = _context.t_YiJianFanKuiBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["id"]));
                if (Request.Form["er"] == "2")
                {
                    updatebase.YingDaNeiRong = updatebase.YingDaNeiRong;
                    updatebase.ZhuangTai = Request.Form["zhuangtai"];
                }
                else
                {
                    updatebase.ZhuangTai = updatebase.ZhuangTai;
                    updatebase.YingDaNeiRong = Request.Form["yingda"];
                }
                panduan = _context.SaveChanges() > 0;
            }


            //var updatebase = it_YiJianFanKuiBiaoService.GetById(Convert.ToInt32(Request.Form["id"]));
            //updatebase.BeiZhu1 = updatebase.BeiZhu1;
            //updatebase.BeiZhu2 = updatebase.BeiZhu2;
            //updatebase.BeiZhu3 = updatebase.BeiZhu3;
            //updatebase.BeiZhu4 = updatebase.BeiZhu4;
            //updatebase.BeiZhu5 = updatebase.BeiZhu5;
            //updatebase.BeiZhu6 = updatebase.BeiZhu6;
            //updatebase.BeiZhu7 = updatebase.BeiZhu7;
            //updatebase.BeiZhu8 = updatebase.BeiZhu8;
            //updatebase.BeiZhu9 = updatebase.BeiZhu9;
            //updatebase.BeiZhu10 = updatebase.BeiZhu10;
            //updatebase.RenYuanId = updatebase.RenYuanId;
            //updatebase.FanKuiNeiRong = updatebase.FanKuiNeiRong;
            //updatebase.ZhuangTai = updatebase.ZhuangTai;
            //updatebase.ShiJian = updatebase.ShiJian;

            //if (Request.Form["er"] == "2")
            //{
            //    updatebase.YingDaNeiRong = updatebase.YingDaNeiRong;
            //    updatebase.ZhuangTai = Request.Form["zhuangtai"];
            //}
            //else
            //{
            //    updatebase.ZhuangTai = updatebase.ZhuangTai;
            //    updatebase.YingDaNeiRong = Request.Form["yingda"];
            //}
            //it_YiJianFanKuiBiaoService.Update(updatebase)
            if (panduan)
            {
                return Convert.ToInt32(Request.Form["id"]);
            }
            else
            {
                return -1;
            }
        }
        #endregion
    }
}
